//
//  WDLoanCell.m
//  Zhihuijinfu
//
//  Created by 王笛 on 2016/12/3.
//  Copyright © 2016年 王笛. All rights reserved.
//

#import "WDLoanCell.h"
#import "WDLoan.h"
#import "UIColor+WDColorExtension.h"
#import "WDPhoneAlertView.h"

@interface WDLoanCell ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *LoanPersonLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UILabel *carPriceLabel;
@property (weak, nonatomic) IBOutlet UILabel *LoanAmountLabel;
@property (weak, nonatomic) IBOutlet UILabel *payPeriodsLabel;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@end

@implementation WDLoanCell

+ (instancetype)loanCellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"loanCell";
    WDLoanCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] lastObject];
    }
    return cell;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.layer.cornerRadius = 4;
    self.layer.masksToBounds = YES;
    
}

- (IBAction)clickPhoneBtn:(id)sender {
    
    WDPhoneAlertView *alert = [[WDPhoneAlertView alloc] initWithTitle:@"呼叫" message:self.phoneLabel.text sureBtn:@"呼叫" cancleBtn:@"取消"];
    
    __weak typeof (self)weakSelf = self;
    
    alert.resultIndex = ^(NSInteger index){
        
        NSURL *openURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", weakSelf.phoneLabel.text]];
        
        [[UIApplication sharedApplication] openURL:openURL];
    };
    
    [alert show];
    
}

- (void)setLoanData:(WDLoan *)loanData
{
    _loanData = loanData;
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:loanData.status_color] placeholderImage:WDDefaultImage];
    self.statusLabel.text = loanData.status_name;
    
    self.LoanPersonLabel.text = loanData.user_name;
    self.phoneLabel.text = loanData.phone;
    self.carPriceLabel.text = [NSString stringWithFormat:@"%.2f 万", [loanData.car_price floatValue]];
    self.LoanAmountLabel.text = [NSString stringWithFormat:@"%.2f 万", [loanData.loan_amount floatValue]];
    self.payPeriodsLabel.text = [NSString stringWithFormat:@"%zd 期", [loanData.pay_periods integerValue]];
}

@end
